<?php
function htmlspec($src) {
	return htmlspecialchars($src, ENT_COMPAT, 'UTF-8');
}

function make_blog_url($blogger_id, $user_url) {
	if(is_null($user_url) || strlen($user_url) == 0) {
		return '/' . $blogger_id;
	} else {
		return '/' . $user_url;
	}
}

function get_avatar_name($user_id, $rand = FALSE) {
	$fn = $_SERVER['DOCUMENT_ROOT'] . '/img/avatars/avt_' . $user_id . '_48x48.jpg';
	if(file_exists($fn)) {
		if($rand) {
			return 'avt_' . $user_id . '_48x48.jpg?R=' . rand();
		} else {
			return 'avt_' . $user_id . '_48x48.jpg';
		}
	} else {
		return 'avt_-1_48x48.jpg';
	}
}

function date_part($str_datetime) {
	$dp = date_parse($str_datetime);
	return $dp['month'] . '/' . $dp['day'] . '/' . $dp['year'];
}

function entry_state_indicator($entry_state, $smaller = FALSE) {
	$r = ' ';
	switch($entry_state) {
		case 0:
			$r .= 'secret';
			break;
		case 3:
			$r .= 'community';
			break;
		case 99:
			$r .= 'draft';
			break;
		default:
			$r = '';
			break;
	}
	if($smaller && (strlen($r) >= 1)) {
		$r .= '_small';
	}
	return $r;
}

function show_comment_author($author_name, $url) {
	if(strlen($url) > 0) {
		return ' <a href="' . $url . '" target="_blank">' . htmlspec($author_name) . '</a>';
	} else {
		return htmlspec($author_name);
	}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<title><?php echo(htmlspec($entry_info->title . ' - ' . $blog_info->title)); ?></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/css/iphone.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/ubb_more.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/expressions.css" media="screen" />
</head>
<body>
	<div id="wrapper">
		<div id="hdr1">
			<img alt="" class="userAvatar" src="/img/avatars/<?php echo(get_avatar_name($blog_info->id)); ?>" />
			<h1 class="fadeLong"><?php echo(htmlspec($blog_info->title)); ?></h1>
			<div id="operMenuHandle"><a href="javascript:;">Menu▼</a></div>
			<ul id="operMenu">
				<li><a href="/">返回社区</a></li>
				<li><a href="<?php echo('/' . $blogger_uri); ?>">Blog 首页</a></li>
				<?php
				if($is_blog_owner) {
				?>
				<li><a href="<?php echo('/' . $blogger_uri); ?>/mgmt/logout">Logout</a></li>
				<?php
				} else {
				?>
				<li><a href="<?php echo('/' . $blogger_uri); ?>/mgmt/login">登录</a></li>
				<?php
				}
				?>
			</ul>
		</div>
		<div id="bdy1">
		
			<div id="jumper">
				<?php if(!is_null($prev_entry_info)) { ?>
				<div id="prev_entry"><a href="<?php echo('/' . $blogger_uri . '/entry/' . $prev_entry_info->id); ?>">&laquo; 前一篇</a></div>
				<?php } else { ?>
				<div id="prev_entry">&nbsp;</div>
				<?php } ?>
				<?php if(!is_null($next_entry_info)) { ?>
				<div id="next_entry"><a href="<?php echo('/' . $blogger_uri . '/entry/' . $next_entry_info->id); ?>">后一篇 &raquo;</a></div>
				<?php } else { ?>
				<div id="next_entry">&nbsp;</div>
				<?php } ?>
				<div class="z"></div>
			</div>
			
			<div class="entry">
				<h3 class="expr_<?php echo($entry_info->expression); ?>">
					<?php if($entry_info->id > 0) { ?>
					<?php echo(htmlspec($entry_info->title)); ?>
					<?php } else { ?>
					<?php echo($entry_info->state == 0 ? '保密网志' : '社区内部网志'); ?>
					<?php } ?>
				</h3>
				
				<div class="content<?php echo(entry_state_indicator($entry_info->state)); ?>">
					<?php echo($entry_info->content); ?>
				</div>
				
				<div class="info">
					发布于 <?php echo($entry_info->post_time); ?>
					|
					<?php if($entry_info->id > 0) { ?>
					<a href="<?php echo('/' . $blogger_uri . '/entry/' . $entry_info->id . '#comments'); ?>">评论：<strong><?php echo($entry_info->comment_num); ?></strong></a>
					<?php } else { echo('评论：N/A'); } ?>
				</div>
			</div>
				
			<?php if(($is_blog_owner) || ($entry_info->state == 1) || ($entry_info->state == 0 && $is_blog_friend) || ($entry_info->state == 3 && $is_community_member)) { ?>
			<div id="comments">
				<div id="comments_wrapper">
				<?php
				$is_odd = TRUE;
				foreach($comments as $cmt) {
				?>
				<div id="c<?php echo($cmt->id); ?>" class="comment <?php echo($is_odd ? 'odd' : 'even'); ?>">
					<div class="a"><img alt="" src="<?php echo(base_url() . 'img/avatars/' . get_avatar_name($cmt->by_blogger_id)); ?>" /></div>
					<div class="c">
						<div class="i">
							<?php echo(show_comment_author($cmt->author_name, $cmt->url) . ' @ ' . $cmt->post_time . $cmt->ip); ?>
						</div>
						<?php echo($cmt->content); ?>
					</div>
				</div>
				<?php
					$is_odd = !$is_odd;
				}
				?>
				</div><!-- /comments_wrapper -->
				
			</div><!-- /comments -->
			<?php } ?>
			
			<div id="commentArea">
				<?php if($entry_info->allow_comment) { ?>
				<div id="commentBox">
					<div id="closeBox"><a href="javascript:;">X</a></div>
					<form>
						<label for="txtAuthorName">你的大名：<span class="hint"><?php if($current_user_id > 0) { echo('aka ' . htmlspec($current_user_name)); } ?></span></label>
						<input type="text" id="txtAuthorName" class="textBox" value="<?php echo($last_comment_name); ?>" maxlength="16" />
						
						<label for="txtCommentContent">说点什么：</label>
						<textarea id="txtCommentContent" class="textBox" rows="3"></textarea>
						
						<input type="submit" id="btnPostComment" class="button" value="发表" />
						<img id="imgWaitComment" alt="Loading" src="/img/ajax-loader.gif" border="0" width="43" height="11" style="vertical-align:middle;display:none;" />
						<input type="hidden" id="hidCurrentEntryId" value="<?php echo($entry_info->id); ?>" />
					</form>
				</div>
				<div id="commentHandle"><a href="javascript:;">Comment▲</a></div>
				<?php } else { ?>
				<h4 style="color:#000;">评论已关闭！</h4>
				<?php } ?>
			</div>
		
		</div>
		<?php
		include('common_footer.php');
		?>
	</div>
<!-- JS -->
<script type="text/javascript" src="/js/3rd/jquery.pack.js"></script>
<script type="text/javascript" src="/js/3rd/json2.js"></script>
<script type="text/javascript" src="/js/iphone.js"></script>
<script type="text/javascript">
var g_userSiteRoot = "<?php echo('/' . $blogger_uri); ?>";

function addCommentNum(num) {
	var oldCommentNum = parseInt($("div.entry div.info strong").text());
	oldCommentNum += num;
	$("div.entry div.info strong").text(oldCommentNum.toString());
}

function openCommentBox() {
	$("#commentBox").show();
	$("#commentHandle").css('visibility', 'hidden');
}

function closeCommentBox() {
	$("#commentBox").hide();
	$("#commentHandle").css('visibility', 'visible');
}

$(document).ready(function() {
	initPage4iPhone();
	
	$("#operMenuHandle").click(function() {
		$("#operMenu").toggle();
	});
	
	// post comment
	$("#btnPostComment").click(function() {
		var entry_id = $("#hidCurrentEntryId").val();
		if(entry_id == "") {
			alert("Unknown error!");
			return false;
		}
		
		var author_name = $("#txtAuthorName").val();
		if(author_name != "") {
			if(/[^\u4E00-\u9FA0a-zA-Z0-9._ ]/.test(author_name)) {
				alert("名字中不能含有除中文、字母、数字、句点及下划线之外的字符！");
				return false;
			}
		}
		
		var content = $("#txtCommentContent").val();
		if(content == "") {
			alert("评论内容不能为空！");
			return false;
		}
		
		$("#imgWaitComment").show();
		$(this).attr("disabled", "disabled");
		var commentData = {
			"author_name": author_name,
			"email": "",
			"url": "",
			"content": content
		}
		$.post(g_userSiteRoot + "/comment/" + entry_id, {
			"hidIsPostBack": "ja",
			"posted_data": JSON.stringify(commentData)
		}, function(rdata) {
			$("#imgWaitComment").hide();
			$("#btnPostComment").removeAttr("disabled");
			rdata = rdata.split("_");
			if(rdata[0] != "SUCC") {
				alert("Operation failed!");
				return false;
			}
			
			closeCommentBox();
			
			$("#txtCommentContent").val("");
			$("#comments_wrapper").load(g_userSiteRoot + "/comments_list/" + entry_id, function() {
//				registerCommentsEvents();
				addCommentNum(1);
			});
		});
		return false;
	});
	
	$("#commentHandle a").click(function() {
		openCommentBox();
	});
	
	$("#closeBox a").click(function() {
		closeCommentBox();
	});
});
</script>
<!-- /JS -->
</body>
</html>